package aa.bb.client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberInputStream;
import java.io.LineNumberReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.SequenceInputStream;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Date;

import org.apache.commons.io.output.FileWriterWithEncoding;



public class cccc {
	static File file=new File("C:\\Documents and Settings\\yuliang\\桌面\\b.txt");
	static FileInputStream fin = null;
	static FileOutputStream fout=null;
	static BufferedInputStream bin=null;
	static BufferedOutputStream bout=null;
	static InputStreamReader inReader=null;
	static OutputStreamWriter outWriter=null;
	static FileReader reader=null;
	static FileWriter writer=null;
	static BufferedReader breader=null;
	static BufferedWriter bwriter=null;
	
	static ObjectInputStream oin=null;
	static ObjectOutputStream oout=null;
    public static void main(String[] args)   
    {  
    	init();
    	try {
    		//System.out.println("🂓".getBytes("").length);
			//useNormalStream();
			//useBufferedStream();
			//useReader();
			//useFileReader();
			//useBufferedReader();
			//useObject();
    		Class.forName("java.io.BufferedReader");
    		writer.write("h呼哈哈です11111111");
    		writer.close();
    		LineNumberReader reader=new LineNumberReader(inReader);
    		System.out.println(reader.readLine());
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private static void useObject() throws IOException, ClassNotFoundException {
		Student student=new Student();
		student.setId(2);
		student.setName("呼哈哈");
		oout.writeObject(student);
		oout.close();
		Student result=(Student)oin.readObject();
		oin.close();
		System.out.println(result.getId());
	}

	private static void useBufferedReader() throws IOException {
		bwriter.write("です中國");
		bwriter.close(); 
		char[] buffer=new char[30];
		//breader.read(buffer);
		System.out.println(breader.readLine());
		breader.close();
		//System.out.println(new String(buffer));
		
	}

	private static void useFileReader() throws IOException {
		writer.write("日語".toCharArray());
		writer.close();
		char[] buffer=new char[30];
		reader.read(buffer);
		inReader.close();
		System.out.println(new String(buffer));
	}

	private static void useReader() throws IOException {
		outWriter.write("𪜁"); 
		outWriter.close(); 
		//System.out.println(outWriter.getEncoding());
		char[] buffer=new char[30];
		int n=inReader.read(buffer);
		inReader.close();
		System.out.println(new String(buffer));
		
	}
	private static void useBufferedStream() throws UnsupportedEncodingException, IOException {
		bout.write("ました".getBytes("utf-8"));
		bout.close();
		byte[] buffer=new byte[30];
		bin.read(buffer);
		bin.close();
		System.out.println(new String(buffer,0,"ました".getBytes("utf-8").length,"utf-8"));
	}
    
	public static void useNormalStream() throws UnsupportedEncodingException, IOException{
    	fout.write("です中".getBytes("utf-8"));
    	fout.close();
		byte[] buffer=new byte[30];
		fin.read(buffer);
		fin.close();
		System.out.println(new String(buffer,"utf-8"));
    }
	private static void init() {
		if (!file.exists()) {
    		try {
    			file.createNewFile();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
        try {
			fin=new FileInputStream(file);
			fout=new FileOutputStream(file);
			bin=new BufferedInputStream(fin);
			bout=new BufferedOutputStream(fout);
			inReader=new InputStreamReader(fin);
			outWriter=new OutputStreamWriter(fout);
			writer=new FileWriter(file);
			reader=new FileReader(file); 
			breader=new BufferedReader(reader); //可以用另一個對象做參數
			bwriter=new BufferedWriter(writer);
			
			oout=new ObjectOutputStream(fout);
			oin=new ObjectInputStream(fin);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}  
}